//
//  ApplyView.m
//  项目三
//
//  Created by rimi on 15/10/14.
//  Copyright (c) 2015年 rimi. All rights reserved.
// 0 20 701 904    600 40 88 766  615 431  78 161  123

#import "ApplyView.h"
#import "UserInfoView.h"
#import "ContactView.h"
#import "OtherInfoView.h"
#import "JobInfoView.h"

@interface ApplyView () {
    
    UIView *_currentView;
    UIButton *_btn;
    UIView *_btnView;
    
    UserInfoView *_userView;
    ContactView *_contactView;
    OtherInfoView *_otherView;
    JobInfoView *_jobView;
}

- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation ApplyView


- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        [self initializeUserInterface];
    }
    return self;
}

- (void)initializeUserInterface {
    
    self.backgroundColor = COLOR(205, 205, 205, 1);
    
    _userView = [[UserInfoView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 708, 922))];
    [self addSubview:_userView];
    [_userView.userBtn addTarget:self action:@selector(respondsToNext:) forControlEvents:UIControlEventTouchUpInside];
    _userView.userBtn.tag = 105;
    
    _btnView = [[UIView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(597, 40, 88, 766))];
    [self addSubview:_btnView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 88, 766))];
    imageView.image = [UIImage imageNamed:@"资料填写竖线"];
    [_btnView addSubview:imageView];
    
    for (int i = 3; i >= 0; i --) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:DHFlexibleFrame(CGRectMake(20, 15 + i * 123, 78, 161))];
        [btn setBackgroundImage:[UIImage imageNamed:BTNIMAGE[i]] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:BTNSIMAGE[i]] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(respondsToBtn:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTag:i + 100];
        if (i == 0) {
            _btn = btn;
            _btn.selected = YES;
        }
        [_btnView addSubview:btn];
    }
}

- (void)respondsToBtn:(UIButton *)sender {

    if (sender == _btn) {
        return;
    }
    switch (sender.tag) {
        case 100: {
            _userView = [[UserInfoView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 708, 922))];
            [self addSubview:_userView];
            [_userView.userBtn addTarget:self action:@selector(respondsToNext:) forControlEvents:UIControlEventTouchUpInside];
            _userView.userBtn.tag = 105;
            
            if (_contactView) {
                [_contactView removeFromSuperview];
            }
            if (_otherView) {
                [_otherView removeFromSuperview];
            }
            if (_jobView) {
                [_jobView removeFromSuperview];
            }
        }
            break;
        case 101: {
            _jobView = [[JobInfoView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 708, 922))];
            [self addSubview:_jobView];
            [_jobView.userBtn addTarget:self action:@selector(respondsToNext:) forControlEvents:UIControlEventTouchUpInside];
            _jobView.userBtn.tag = 106;
            [_jobView.beforeBtn addTarget:self action:@selector(respondsToLast:) forControlEvents:UIControlEventTouchUpInside];
            _jobView.beforeBtn.tag = 108;
            if (_contactView) {
                [_contactView removeFromSuperview];
            }
            if (_otherView) {
                [_otherView removeFromSuperview];
            }
            if (_userView) {
                [_userView removeFromSuperview];
            }
        }
            break;
        case 102: {
            _contactView = [[ContactView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 708, 922))];
            [self addSubview:_contactView];
            [_contactView.userBtn addTarget:self action:@selector(respondsToNext:) forControlEvents:UIControlEventTouchUpInside];
            _contactView.userBtn.tag = 107;
            [_contactView.beforeBtn addTarget:self action:@selector(respondsToLast:) forControlEvents:UIControlEventTouchUpInside];
            _contactView.beforeBtn.tag = 109;
            if (_userView) {
                [_userView removeFromSuperview];
            }
            if (_otherView) {
                [_otherView removeFromSuperview];
            }
            if (_jobView) {
                [_jobView removeFromSuperview];
            }
        }
            break;
        case 103: {
            _otherView = [[OtherInfoView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 708, 922))];
            [_otherView.finishBtn addTarget:self action:@selector(respondsToFinishBtn:) forControlEvents:UIControlEventTouchUpInside];
            [_otherView.beforeBtn addTarget:self action:@selector(respondsToLast:) forControlEvents:UIControlEventTouchUpInside];
            _otherView.beforeBtn.tag = 110;
            [self addSubview:_otherView];
            if (_userView) {
                [_userView removeFromSuperview];
            }
            if (_contactView) {
                [_contactView removeFromSuperview];
            }
            if (_jobView) {
                [_jobView removeFromSuperview];
            }
        }
            break;
        default:
            break;
    }
    [self bringSubviewToFront:_btnView];
    
    sender.selected = YES;
    _btn.selected = NO;
    _btn = sender;
}
- (void)respondsToLast:(UIButton *)sender {

    [self selectAtIndex:sender.tag - 108];
}
- (void)respondsToFinishBtn:(UIButton *)sender {

    
}
- (void)respondsToNext:(UIButton *)sender {

    [self selectAtIndex:sender.tag - 104];
}
- (void)selectAtIndex:(NSInteger)index {

    UIButton *sender = (UIButton *)[self viewWithTag:index + 100];
    
    if (sender == _btn) {
        return;
    }
    switch (sender.tag) {
        case 100: {
            _userView = [[UserInfoView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 708, 922))];
            [self addSubview:_userView];
            [_userView.userBtn addTarget:self action:@selector(respondsToNext:) forControlEvents:UIControlEventTouchUpInside];
            _userView.userBtn.tag = 105;
            
            if (_contactView) {
                [_contactView removeFromSuperview];
            }
            if (_otherView) {
                [_otherView removeFromSuperview];
            }
            if (_jobView) {
                [_jobView removeFromSuperview];
            }
        }
            break;
        case 101: {
            _jobView = [[JobInfoView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 708, 922))];
            [self addSubview:_jobView];
            [_jobView.userBtn addTarget:self action:@selector(respondsToNext:) forControlEvents:UIControlEventTouchUpInside];
            _jobView.userBtn.tag = 106;
            [_jobView.beforeBtn addTarget:self action:@selector(respondsToLast:) forControlEvents:UIControlEventTouchUpInside];
            _jobView.beforeBtn.tag = 108;
            if (_contactView) {
                [_contactView removeFromSuperview];
            }
            if (_otherView) {
                [_otherView removeFromSuperview];
            }
            if (_userView) {
                [_userView removeFromSuperview];
            }
        }
            break;
        case 102: {
            _contactView = [[ContactView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 708, 922))];
            [self addSubview:_contactView];
            [_contactView.userBtn addTarget:self action:@selector(respondsToNext:) forControlEvents:UIControlEventTouchUpInside];
            _contactView.userBtn.tag = 107;
            [_contactView.beforeBtn addTarget:self action:@selector(respondsToLast:) forControlEvents:UIControlEventTouchUpInside];
            _contactView.beforeBtn.tag = 109;
            if (_userView) {
                [_userView removeFromSuperview];
            }
            if (_otherView) {
                [_otherView removeFromSuperview];
            }
            if (_jobView) {
                [_jobView removeFromSuperview];
            }
        }
            break;
        case 103: {
            _otherView = [[OtherInfoView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 708, 922))];
            [_otherView.finishBtn addTarget:self action:@selector(respondsToFinishBtn:) forControlEvents:UIControlEventTouchUpInside];
            [_otherView.beforeBtn addTarget:self action:@selector(respondsToLast:) forControlEvents:UIControlEventTouchUpInside];
            _otherView.beforeBtn.tag = 110;
            [self addSubview:_otherView];
            if (_userView) {
                [_userView removeFromSuperview];
            }
            if (_contactView) {
                [_contactView removeFromSuperview];
            }
            if (_jobView) {
                [_jobView removeFromSuperview];
            }
        }
            break;
        default:
            break;
    }
    
    [self bringSubviewToFront:_btnView];
    sender.selected = YES;
    _btn.selected = NO;
    _btn = sender;
}
@end
